package com.corner.ui.activity;

import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.corner.R;
import com.corner.model.cornerservice.domain.Attention;
import com.corner.presenter.imp.AttentionPresenter;
import com.corner.ui.adapter.AttentionAdapter;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.view.IAttentionView;
import com.corner.utils.AppManager;
import com.corner.utils.view.FindView;
import com.corner.utils.view.MainActionBar;
import com.corner.utils.view.widget.XListView;
import com.daimajia.swipe.SwipeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/25，11.35.
 * 描述：
 */
public class AttentionActivity extends BaseActivity<AttentionPresenter> implements IAttentionView, View.OnClickListener {
    private MainActionBar actionBar;
    @FindView(R.id.lv_location_list)
    private XListView lvLocations;
    private AttentionAdapter adapter;
    private List<Attention> attentions;

    @Override
    protected AttentionPresenter createP() {
        return new AttentionPresenter();
    }

    @Override
    protected void setOnClick() {
        actionBar.btnLeft.setOnClickListener(this);
        lvLocations.setPullLoadEnable(false);
        lvLocations.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                presenter.requestAttention();
            }

            @Override
            public void onLoadMore() {
                presenter.loadMoreAttention();
            }
        });
        lvLocations.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                int position = lvLocations.pointToPosition(x, y);
                closeSwipe(position);
                return false;
            }
        });
    }

    /**
     * 关闭滑动
     *
     * @param position
     */
    public void closeSwipe(int position) {
        for (int i = 0; i < lvLocations.getChildCount(); i++) {
            if (position != i) {
                View child = lvLocations.getChildAt(i).findViewById(R.id.swipe_frag_session);
                if (child != null) {
                    ((SwipeLayout) child).close(true);
                }
            }
        }
    }

    @Override
    protected void initData() {
        adapter = new AttentionAdapter(this, new ArrayList<Attention>());
        lvLocations.setAdapter(adapter);
        presenter.requestAttention();
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_attention_channel);
    }

    @Override
    public void initView() {
        super.initView();
        actionBar = new MainActionBar(this);
        actionBar.initActionBarButton();
        actionBar.btnLeft.setVisibility(View.VISIBLE);
        actionBar.btnLeft.setText(R.string.go_back);
    }

    @Override
    public void showRequestAttention(List<Attention> attentions) {
        this.attentions = attentions;
        adapter.changeList(attentions);
        closeSwipe(-1);
    }

    @Override
    public void requestComplete() {
        lvLocations.stopRefresh();
    }

    @Override
    public void setLoadMoreEnable(boolean enable) {
        lvLocations.setPullLoadEnable(enable);
    }

    @Override
    public void loadMoreComplete() {
        lvLocations.stopLoadMore();
    }

    @Override
    public void showLoadMoreAttention(List<Attention> attentions) {
        this.attentions = attentions;
        adapter.changeList(this.attentions);
        closeSwipe(-1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_actionbar_left:
                AppManager.getInstance().finishActivity(this);
                overridePendingTransition(R.anim.fragment_slide_in_right, R.anim.fragment_slide_out_left);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        closeSwipe(-1);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            AppManager.getInstance().finishActivity(this);
            overridePendingTransition(R.anim.fragment_slide_in_right, R.anim.fragment_slide_out_left);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.onDestroy();
        }
        attentions = null;
        adapter = null;
    }
}
